from typing import Any, Dict, Optional
from pydantic import BaseModel, Field, field_validator

from ...utils import DEFAULT_ENGINE

class RunParams(BaseModel):
    engine: str = Field(default=DEFAULT_ENGINE)
    max_steps: int = 1000
    timeout: Optional[float] = None
    parallelism: Optional[int] = 1
    context: Dict[str, Any] = Field(default_factory=dict)

    @field_validator("engine")
    @classmethod
    def _validate_engine(cls, v: str) -> str:
        v = v.strip()
        allowed = {"flow", "lang", "demo"}
        if v not in allowed:
            raise ValueError(f"engine must be one of {allowed}")
        return v

    @field_validator("max_steps")
    @classmethod
    def _validate_max_steps(cls, v: int) -> int:
        if v <= 0:
            raise ValueError("max_steps must be positive")
        return v

    @field_validator("parallelism")
    @classmethod
    def _validate_parallelism(cls, v: Optional[int]) -> Optional[int]:
        if v is not None and v <= 0:
            raise ValueError("parallelism must be positive when provided")
        return v